{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>编辑考核项</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" {$performance_data.type==2?'checked':""} value="2" title="填空题" checked>
                <input type="radio" name="type" {$performance_data.type==1?'checked':""} value="1" title="选择题">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">考核项名称</label>
            <div class="layui-input-4">
                <input type="text" id="title" name="title" lay-verify="required" value="{$performance_data.title}"  class="layui-input">
            </div>
        </div>

        <div class="p">
            {volist name="performance_data.var_content" id="vo" key="k"}
            <div class="layui-form-item variable" >

                <div class="layui-inline">
                    <label class="layui-form-label">变量描述</label>
                    <div class="layui-input-inline">
                        <input type="text" lay-verify="required" value="{$key}" class="layui-input des{$k}">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">分值</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <select name="operator"  class='operator{$k}' lay-verify="operator" style="font-size: 5em;color: red">
                            <option value="+" {$vo.op=='+'?'selected':""}>&nbsp;&nbsp;+&nbsp;&nbsp;</option>
                            <option value="-" {$vo.op=='-'?'selected':""}>&nbsp;&nbsp;-&nbsp;&nbsp;</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <input type="number" step="0.1" lay-verify="required" value="{$vo.score}" class="layui-input score{$k}">
                    </div>
                </div>
                <div class="layui-inline">
                    <input type="button" value="移除" class="layui-btn layui-btn-primary rem">
                </div>

            </div>
            {/volist}
        </div>
        <input type="hidden" id="title_id" value="{$performance_data.title_id}">
    </form>

    <div style="margin-left: 852px;margin-top: -58px;">
        <button class="layui-btn layui-btn-primary additem" >添加考核项</button>
    </div>

    <div class="layui-form-item" style="margin-top: 50px">
        <div class="layui-input-block" id="test">

            <button class="layui-btn" onclick="save()">保存</button>
            <a  href="{:url('performanceAdd')}" class="layui-btn layui-btn-danger">清空</a>
            <a href="{:url('lists')}" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            <!--<a href="{:url('instruction')}" class="layui-btn layui-btn-primary">说明事例</a>-->
        </div>
    </div>
</div>
{include file="common/foot"/}
<script>

    var j = {$count}+1;

    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;
        form.render();

        $(document).on('click','.additem',function () {

            var item = "<div class=\"layui-form-item variable\" >\n" +
                "\n" +
                "                <div class=\"layui-inline\">\n" +
                "                    <label class=\"layui-form-label\">变量描述</label>\n" +
                "                    <div class=\"layui-input-inline\">\n" +
                "                        <input type=\"text\" lay-verify=\"required\" placeholder=\"{:lang('pleaseEnter')}变量描述\" class=\"layui-input des"+j+"\">\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "\n" +
                "                <div class=\"layui-inline\">\n" +
                "                    <label class=\"layui-form-label\">分值</label>\n" +
                "                    <div class=\"layui-input-inline\" style=\"width: 100px;\">\n" +
                "                        <select name=\"operator\" class='operator"+j+"' lay-verify=\"operator\">\n" +
                "                            <option value=\"+\">+</option>\n" +
                "                            <option value=\"-\">-</option>\n" +
                "                        </select>\n" +
                "                    </div>\n" +
                "                    <div class=\"layui-input-inline\">\n" +
                "                        <input type=\"number\" step=\"0.1\" lay-verify=\"required\" placeholder=\"{:lang('pleaseEnter')}分值\" class=\"layui-input score"+j+"\">\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "\n" +
                "                <div class=\"layui-inline\">\n" +
                "                    <input type=\"button\" value=\"移除\" class=\"layui-btn layui-btn-primary rem\">\n" +
                "                </div>\n" +
                "\n" +
                "            </div>";
            j++;
            $(".p").append(item);
            form.render();

        });

        $(document).on('click','.rem',function () {
            $(this).parent('div').parent('div').remove();
        });


    });

    function save() {

        var title=$("#title").val();
        var type =$('[name=type]:checked').val();

        if(title==''){
            layer.msg("考核项名称不能为空", {icon: 2});
            return;
        }

        var numArrAll = []; // 定义一个空数组

        for( var i = 1 ; i < j ; i++ ){

            /* console.log('des--------->'+i);
             console.log($('.des'+i).val());
             console.log('operator--------->'+i);
             console.log($('.operator'+i).val());
             console.log('score--------->'+i);
             console.log($('.score'+i).val());*/



            var des         = $.trim($('.des'+i).val());
            var operator    = $.trim($('.operator'+i).val());
            var score       = $.trim($('.score'+i).val());

            if((des!=undefined)&&(operator!=undefined)&&(score!=undefined)&&(score!='')&&(des!='')){

                var numArr = {}; // 定义一个空对象
                numArr['des'] = des;
                numArr['operator'] = operator;
                numArr['score'] = score;
                numArrAll.push(numArr);

            }else{

                layer.msg("请完善所有变量描述及分值", {icon: 2});

            }


        }

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "{:url('performanceEdit')}",
            data: {title:title,var_content:numArrAll,type:type,title_id:$("#title_id").val()},
            success: function(res){
                if(res.code) {
                    layer.msg(res.msg, {icon: 1});
                    // location.href = "{:url(lists)}";
                    // $('.onlina').remove();
                    // $('#test').append("<a href=\"{:url('online_test')}?title_id="+res.data+"\"  class=\"layui-btn layui-btn-primary onlina\">在线测试</a>");
                }else{
                    layer.msg(res.msg, {icon: 2});
                }
            },
        });
    }

</script>